//
//  PersonalInfoVC.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/6/19.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "PersonalInfoVC.h"
#import "PersonInfoCell.h"
#import "SexViewController.h"

@interface PersonalInfoVC ()<SexViewControllerDelegate>
{
    NSArray *titleArr;
    UIImage *headImage;
    NSString *userSex; //性别
    NSString *datePickerTime; //出生年月
}

@end

@implementation PersonalInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置返回按钮
    [self.backButton setImage:[UIImage imageNamed:@"back_white_btn"] forState:UIControlStateNormal];
    [self.backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
    headImage = [[UIImage alloc] init];
    titleArr = [[NSArray alloc] initWithObjects:@"头像",@"账户",@"性别",@"出生日期", nil];
    self.tableview.separatorStyle = NO;
    userSex = @"保密";
    
    //datepicker 的设置
    self.dateView.hidden = YES;
    self.datePickerView.datePickerMode = UIDatePickerModeDate;
    //设置为中文模式
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"];
    self.datePickerView.locale = locale;
    [self.datePickerView addTarget:self action:@selector(getDateViewData:) forControlEvents:UIControlEventValueChanged];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置状态栏颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.tabBarController.tabBar.hidden = YES;
}

- (IBAction)backButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)pickerButton:(id)sender
{
    //隐藏datePickerView
    self.dateView.hidden = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.dateView.hidden = YES;
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonInfoCell *cell;
    
    static NSString *cellIdentifier = @"personalCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"PersonInfoCell" owner:self options:nil][0];
    }
    
    cell.headLabel.text = [titleArr objectAtIndex:indexPath.row];
    //cell 没有选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0)
    {
        cell.infoLabel.hidden = YES;
        cell.headerImage.image = [FRUtils circleImage:headImage withParam:1];
    }
    else if (indexPath.row == 1)
    {
        cell.nextImage.hidden = YES;
        cell.infoLabel.text = @"dd25251";
    }
    else if (indexPath.row == 2)
    {
         cell.infoLabel.text = userSex;
        cell.headerImage.hidden = YES;

    }
    else
    {
        cell.infoLabel.text = datePickerTime;
        cell.headerImage.hidden = YES;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        
        self.dateView.hidden = YES;
        
        UIActionSheet *photosSheet = [[UIActionSheet alloc]initWithTitle:@"上传头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
        photosSheet.actionSheetStyle = UIActionSheetStyleDefault;
        [photosSheet showInView:self.view.window];

    }
    else if (indexPath.row == 2)
    {
        self.dateView.hidden = YES;

        SexViewController *sexVC = [[SexViewController alloc] init];
        sexVC.delegate = self;
        [self.navigationController pushViewController:sexVC animated:YES];
    }
    else if (indexPath.row == 3)
    {
        self.dateView.hidden = NO;
    }
    else
    {
        self.dateView.hidden = YES;
    }
}

#pragma mark - datePickerView
//datePickerView 调用方法
-(void)getDateViewData:(id)sender
{
    UIDatePicker *picker = sender;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    datePickerTime = [dateFormatter stringFromDate:picker.date];
    [self.tableview reloadData];
}


#pragma mark - SexViewControllerDelegate
//回调性别设置方法
-(void)choseSexDelegate:(NSString *)sex
{
    userSex = sex;
    [self.tableview reloadData];
}

#pragma mark - UIImagePickerControllerDelegate
//调起相机以及读取相册代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    headImage = image;
    [self.tableview reloadData];
    
    //发送请求，上传图片
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}


#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSUInteger sourceType = 0;
    switch (buttonIndex) {
        case 0:
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                sourceType = UIImagePickerControllerSourceTypeCamera;
            }else{
                [Dialog simpleToast:@"此设备不支持拍照功能!" withDuration:1.2];
                return;
            }
            break;
        case 1:
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            break;
        default:
            return;
            break;
    }
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
